<?php
App::uses('Sanitize','Utility');
class OrdersController extends AppController {	
	var $uses = array('Order','ShippingType','PaymentType','Stock','City','District','Area','OrderShipping');		
    public function history() {
		$this->Order->recursive = 0;
        $rs = $this->Order->find('all',array(
			'fields' => array('Order.id','Order.created','Order.total','OrderStatus.name'),
			'conditions'=>array(				
				'customer_id'=>$this->Auth->user('id'),
				'Order.del_flag'=>0,				
			),			
		));
		$this->set(compact('rs'));
    }
	
	public function detail($id = null){
		if($id == null) $this->redirect('/danh-sach-don-hang');
		$this->Order->recursive = 1;
		$rs = $this->Order->find('first',array(			
			'conditions' => array(
				'Order.id' => Sanitize::escape($id),
			),
		));
		$this->set(compact('rs'));
	}
	
	public function loadDistrict($city_id = null){
		$this->render(false);
		if($this->request->is('ajax') && $city_id != null){
			$city_id = Sanitize::escape($city_id);
			$rs = $this->District->find('list',array(
				'fields'=>array('id','name'),
				'conditions'=>array('city_id'=>$city_id),
				'order'=>'orderby asc',
			));
			echo json_encode($rs);
		}
		exit;
	}
	
	public function loadArea($district_id = null){
		$this->render(false);
		if($this->request->is('ajax') && $district_id != null){
			$district_id = Sanitize::escape($district_id);
			$rs = $this->Area->find('list',array(
				'fields'=>array('id','name'),
				'conditions'=>array('district_id'=>$district_id),
				'order'=>'orderby asc',
			));
			echo json_encode($rs);
		}
		exit;
	}
	
	public function checkout(){		
		if(!empty($this->data)){
			$this->Session->write('order.shipping',$this->data['OrderDetail']);
			$this->redirect('shipping');			
		}
		if($this->Session->check('order.shipping')){
			$this->request->data = $this->Session->read('order.shipping');				
		}
		$cities = $this->City->find('list',array('fields'=>array('id','name'),'order'=>'orderby asc'));
		$this->set(compact('cities'));
	}
	
	public function shipping(){
		if(!$this->Session->check('order.shipping')){
			$this->Session->setFlash('Xin vui lòng nhập địa chỉ nhận hàng');
			$this->redirect('checkout');
		}
		else {
			if(!empty($this->data)){
				$data = $this->Session->read('order.shipping');
				$data['shipping_type_id'] = $this->data['Order']['shipping_type_id'];
				$this->Session->write('order.shipping',$data);
				$this->redirect('billing');
			}			
		}
		$opt_shipping = $this->ShippingType->find('list',array('fields'=>array('id','name')));
		$this->set(compact('opt_shipping'));
	}
	
	public function billing(){
		if(!$this->Session->check('order.shipping')){
			$this->Session->setFlash('Xin vui lòng nhập địa chỉ nhận hàng');
			$this->redirect('checkout');
		}
		if(!$this->Session->check('cart')){
			$this->Session->setFlash('Xin vui lòng chọn sản phẩm trước khi đặt hàng');
			$this->redirect('/');
		}
		if(!empty($this->data) && $this->request->is('ajax')){
			$s = $this->Session->read('order.shipping');
			$cart = $this->Session->read('cart');
			$this->Stock->recursive = 0;
			$arr = array(
				'Order' => array(
					'customer_id' => $this->Auth->user('id'),
					'payment_type_id' => $this->data['payment_type_id'],
					'shipping_type_id' => $s['shipping_type_id'],
					'total' => $this->data['total'],
					'note' => $this->data['note'],					
				),
				'OrderShipping' => array(
					'name' => $s['name'],
					'address' => $s['address'],
					'tel' => $s['tel'],
				),				
			);
			foreach($cart as $item){
				$arr['OrderDetail'][] = array(
					'product_id' => $item['id'],
					'product_code' => $item['product_code'],
					'product_name' => $item['name'],
					'size_name' => $item['size_name'],
					'amount' => $item['amount'],
					'price' => $item['price'],
				);
			}
						
			if($this->Order->saveAssociated($arr)){
				$this->Stock->recursive = 0;
				foreach($cart as $item){
					$this->Stock->updateAll(array('Stock.amount' => "Stock.amount - {$item['amount']}"),array(
						'Stock.product_id'=>$item['id'],
						'Size.name'=>$item['size_name'],
					));
				}
				$this->Session->delete('cart');
				$this->Session->delete('order.shipping');
				//$this->Session->setFlash('Mua hàng thành công');
				/*
				$Email = new CakeEmail();
				$Email->from(array('vthuytrang2000@gmail.com' => 'Shop Minh Thư'));
				$Email->to($data['Customer']['email']);
				$Email->subject('Bạn đã mua sản phẩm ở Shop Minh Thư');
				$Email->sendAs = 'text';
				$Email->send('Hệ thống email đang xây dựng');
				*/
			}
			exit;
		}
		else {		
			$rs = $this->Session->read('order.shipping');
			$shipping = $this->ShippingType->find('first',array('conditions'=>array('id'=>$rs['shipping_type_id'])));
			$opt_payment = $this->PaymentType->find('list',array('fields'=>array('id','name')));
			$cart = $this->Session->read('cart');		
			$this->set(compact('rs','shipping','opt_payment','cart'));
		}
	}
}
